package tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class ForTag extends TagSupport
{
	private static final long serialVersionUID = 6965073974127078454L;
	private PageContext pageContext = null;
	private Tag parent = null;
	private int start;
	private int end;

	public void setStart(int s)
	{
		//System.out.println("ForTag.setStart(" + s + ")");
		start = s;
	}

	public void setEnd(int e)
	{
		//System.out.println("ForTag.setEnd(" + e + ")");
		end = e;
	}

	@Override
	public int doAfterBody() throws JspException
	{
		//System.out.println("ForTag.doAfterBody()");

		if(start < end)
		{
			pageContext.setAttribute( "start", start++ );
			return EVAL_BODY_AGAIN;
		} else {
			return SKIP_BODY;
		}
	}

	@Override
	public int doEndTag() throws JspException
	{
		//System.out.println("doEndTag()");
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException
	{
		//System.out.println("ForTag.doStartTag()");

		if(start < end)
		{
			pageContext.setAttribute( "start", start++ );
			return EVAL_BODY_AGAIN;
		} else {
			return SKIP_BODY;
		}
	}

	@Override
	public Tag getParent()
	{
		return parent;
	}

	@Override
	public void release()
	{


	}

	@Override
	public void setPageContext( PageContext pc )
	{
		pageContext = pc;
	}

	@Override
	public void setParent( Tag t )
	{
		parent = t;
	}
}